/*
 *    Copyright (c) 2018-2025, cloud All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: cloud
 */

package com.fuyao.cloud.space.edifice.controller;

import cn.hutool.core.io.IoUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuyao.cloud.common.core.util.R;
import com.fuyao.cloud.common.excel.annotation.RequestExcel;
import com.fuyao.cloud.common.log.annotation.SysLog;
import com.fuyao.cloud.space.edifice.service.RoomEdificeService;
import com.fuyao.cloud.space.entity.RoomEdifice;
import com.fuyao.cloud.space.vo.ExcelImportEdificeVO;
import lombok.SneakyThrows;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.access.prepost.PreAuthorize;
import com.fuyao.cloud.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;

/**
 * 楼栋管理
 *
 * @author liyue
 * @date 2021-11-12 16:03:22
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/fyroomedifice")
@Api(value = "fyroomedifice", tags = "楼栋管理管理")
public class RoomEdificeController {

    private final RoomEdificeService fyRoomEdificeService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param fyRoomEdifice 楼栋管理
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('edifice_fyroomedifice_view')" )
    public R getFyRoomEdificePage(Page page, RoomEdifice fyRoomEdifice) {
        return R.ok(fyRoomEdificeService.page(page, Wrappers.query(fyRoomEdifice)));
    }

    /**
     * 通过id查询楼栋管理
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('edifice_fyroomedifice_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(fyRoomEdificeService.getById(id));
    }

    /**
     * 新增楼栋管理
     * @param fyRoomEdifice 楼栋管理
     * @return R
     */
    @ApiOperation(value = "新增楼栋管理", notes = "新增楼栋管理")
    @SysLog("新增楼栋管理" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('edifice_fyroomedifice_add')" )
    public R save(@RequestBody RoomEdifice fyRoomEdifice) {
		fyRoomEdificeService.save(fyRoomEdifice);
        return R.ok(fyRoomEdifice);
    }

    /**
     * 修改楼栋管理
     * @param fyRoomEdifice 楼栋管理
     * @return R
     */
    @ApiOperation(value = "修改楼栋管理", notes = "修改楼栋管理")
    @SysLog("修改楼栋管理" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('edifice_fyroomedifice_edit')" )
    public R updateById(@RequestBody RoomEdifice fyRoomEdifice) {
        return R.ok(fyRoomEdificeService.updateById(fyRoomEdifice));
    }

    /**
     * 通过id删除楼栋管理
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除楼栋管理", notes = "通过id删除楼栋管理")
    @SysLog("通过id删除楼栋管理" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('edifice_fyroomedifice_del')" )
    public R removeById(@PathVariable Long id) {
        return R.ok(fyRoomEdificeService.batchDelete(Collections.singletonList(id)));
    }

	/**
     * 通过id删除楼栋管理
     * @param ids ids
     * @return R
     */
    @ApiOperation(value = "批量删除楼栋管理", notes = "批量删除楼栋管理")
    @SysLog("批量删除楼栋管理" )
    @DeleteMapping("/batchDelete" )
    @PreAuthorize("@pms.hasPermission('edifice_fyroomedifice_del')" )
    public R batchDelete(@RequestBody List<Long> ids) {
        return fyRoomEdificeService.batchDelete(ids);
    }

    /**
     * 导出excel 表格
     * @param fyRoomEdifice 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('edifice_fyroomedifice_export')" )
    public List export(RoomEdifice fyRoomEdifice) {
        return fyRoomEdificeService.listEdifice(Wrappers.query(fyRoomEdifice));
    }

	/**
	 * 创建方法。
	 * @return
	 */
	@PostMapping("/create")
    @PreAuthorize("@pms.hasPermission('edifice_fyroomedifice_add')" )
    public R create() {
        return this.fyRoomEdificeService.create();
    }

	/**
	 * 下载本地导入模板
	 * 获取本地（resources）文件
	 * @param fileName 文件名称
	 * @param response 本地文件
	 */
	@SneakyThrows
	@GetMapping("/local/{fileName}")
	public void localFile(@PathVariable String fileName, HttpServletResponse response) {
		ClassPathResource resource = new ClassPathResource("file/" + fileName);
		response.setContentType("application/octet-stream; charset=UTF-8");
		IoUtil.copy(resource.getInputStream(), response.getOutputStream());
	}

	/**
	 * 导入楼栋信信息
	 * @param excelVOList 区域列表
	 * @param bindingResult 错误信息列表
	 * @return R
	 */
	@PostMapping("/import")
	@PreAuthorize("@pms.hasPermission('edifice_fyroomedifice_export')")
	public R importRegional(@RequestExcel List<ExcelImportEdificeVO> excelVOList, BindingResult bindingResult) {
		return this.fyRoomEdificeService.importEdifice(excelVOList, bindingResult);
	}

	/**
	 * 返回树型结构的楼栋信息,此处楼栋树结构是区域 -》楼栋的树结构
	 *
	 * 给前端做树形查询
	 * @return 树集合
	 */
	@GetMapping(value = "/tree")
	public R getTree() {
		return R.ok(fyRoomEdificeService.selectTree());
	}

	/**
	 * 返回树型结构的楼栋信息
	 * 给前端做树形查询
	 * @return 树集合
	 */
	@GetMapping(value = "/edificeTree")
	public R getEdificeTree() {
		return R.ok(fyRoomEdificeService.selectEdificeTree());
	}

}
